package com.example.kotlin.activity

import android.annotation.SuppressLint
import android.content.ContentValues
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.kotlin.R
import com.example.kotlin.util.MyDatabaseHelper
import kotlinx.android.synthetic.main.activity_sqlactivity.addData
import kotlinx.android.synthetic.main.activity_sqlactivity.createDatabase
import kotlinx.android.synthetic.main.activity_sqlactivity.deleteData
import kotlinx.android.synthetic.main.activity_sqlactivity.queryData
import kotlinx.android.synthetic.main.activity_sqlactivity.replaceData
import kotlinx.android.synthetic.main.activity_sqlactivity.updateData
import java.lang.NullPointerException

class SQLActivity : BaseActivity() {
    private  val TAG = "SQLActivity"
    @SuppressLint("Range")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sqlactivity)

        val dbHelper=MyDatabaseHelper(this,"BookStore.db",2)

        createDatabase.setOnClickListener {
            dbHelper.writableDatabase
        }

        addData.setOnClickListener {
            val db=dbHelper.writableDatabase

            val values1=ContentValues().apply {
                put("name","diyihangdaima")
                put("author","guolin")
                put("pages",321)
                put("price",25.65)
            }

            db.insert("Book",null,values1)

            val values2=ContentValues().apply {
                put("name","dierhangdaima")
                put("author","zhanghongyang")
                put("pages",124)
                put("price",18.65)
            }

            db.insert("Book",null,values2)
        }

        updateData.setOnClickListener {

            val db = dbHelper.writableDatabase
            val values=ContentValues()
            values.put("price",100.22)


            db.update("Book",values,"name = ?", arrayOf("dierhangdaima"))
        }

        deleteData.setOnClickListener {

            val db = dbHelper.writableDatabase
            db.delete("Book","pages > ?", arrayOf("200"))
        }

        queryData.setOnClickListener {
            val db = dbHelper.writableDatabase


            val cursor = db.query("Book", null, null, null, null, null, null)
            if (cursor.moveToFirst()){
                do{
                    val name=cursor.getString(cursor.getColumnIndex("name"))
                    val author=cursor.getString(cursor.getColumnIndex("author"))
                    val pages=cursor.getInt(cursor.getColumnIndex("pages"))
                    val price=cursor.getDouble(cursor.getColumnIndex("price"))

                    Log.e(TAG, "onCreate:${name},${author},${pages},${price}")

                }while (cursor.moveToNext())
            }
            cursor.close()
        }

        replaceData.setOnClickListener {
            val db = dbHelper.writableDatabase
            db.beginTransaction()
            try {
                db.delete("Book",null,null)
//                if (true){
//                    throw NullPointerException()
//                }
                val values=ContentValues().apply {
                    put("name","疯狂java")
                    put("author","李刚")
                    put("pages",400)
                    put("price",60.55)
                }

                db.insert("Book",null,values)
                db.setTransactionSuccessful()

            }catch (e:Exception){
                e.printStackTrace()
            }finally {
                db.endTransaction()
            }





        }
    }
}